/*
 ============================================================================
 Name        : set_ip.c
 Author      : lzc
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "config_ini.h"
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>          // included for 'printf()'
#include <sys/time.h>       // included for 'gettimeofday()'
#include <time.h>           // included for 'localtime()'
#include <string.h>
#include <stdio.h>

void delchar( char *str, char c )
{
  int i,j;
  for(i=j=0;str[i]!='\0';i++)
  {
    if(str[i]!=c)//判断是否有和待删除字符一样的字符
    {
      str[j++]=str[i];
    }
  }
  str[j]='\0';//字符串结束
}


int main(void) {
	int reboot_hour;
	int reboot_min;
	int reboot_sec;


	char ini_maintenance[32] = {0};
	char ini_mon[32] = {0};
	char ini_tues[32] = {0};
	char ini_wed[32] = {0};
	char ini_thurs[32] = {0};
	char ini_fri[32] = {0};
	char ini_sat[32] = {0};
	char ini_sun[32] = {0};
	char ini_rebootTimeSelect[32] = {0};
	


#if 1
	/*网络设置*/
    char local_ip[32] = {0};
    configFileStrGet("/boa/my.ini","local_ip",local_ip,32);

	char local_gateway[32] = {0};
    configFileStrGet("/boa/my.ini","local_gateway",local_gateway,32);

	char local_mask[32] = {0};
    configFileStrGet("/boa/my.ini","local_mask",local_mask,32);


    struct in_addr ipv4_addr;
    struct in6_addr ipv6_addr;

        if (inet_pton(AF_INET, local_ip, &ipv4_addr) == 1) {
;
        }
        else
        {
        	sprintf(local_ip,"192.168.1.168");
        	sprintf(local_gateway,"192.168.1.1");
        	sprintf(local_mask,"255.255.255.0");

        	configFileStrSet("/boa/my.ini","local_ip",local_ip);
        	configFileStrSet("/boa/my.ini","local_gateway",local_gateway);
        	configFileStrSet("/boa/my.ini","local_mask",local_mask);

        }




   // printf("local_ip = %s\n",local_ip);
   // printf("local_gateway = %s\n",local_gateway);
   // printf("local_mask = %s\n",local_mask);

    char cmd_IP_Mask[64] = {0};
    char cmd_GW[64] = {0};

    sprintf(cmd_IP_Mask, "ifconfig eth0 %s netmask %s", local_ip, local_mask);
    sprintf(cmd_GW, "route add default gw %s", local_gateway);

    system(cmd_IP_Mask);
    system(cmd_GW);
#endif
	
	while(1)
	{
		configFileStrGet("/boa/my2.ini","maintenance",ini_maintenance,32);
		//printf("ini_maintenance = %s\r\n",ini_maintenance);

		configFileStrGet("/boa/my2.ini","mon",ini_mon,32);
		//printf("ini_mon = %s\r\n",ini_mon);

		configFileStrGet("/boa/my2.ini","tues",ini_tues,32);
		//printf("ini_tues = %s\r\n",ini_tues);

		configFileStrGet("/boa/my2.ini","wed",ini_wed,32);
		//printf("ini_wed = %s\r\n",ini_wed);

		configFileStrGet("/boa/my2.ini","thurs",ini_thurs,32);
		//printf("ini_thurs = %s\r\n",ini_thurs);

		configFileStrGet("/boa/my2.ini","fri",ini_fri,32);
		//printf("ini_fri = %s\r\n",ini_fri);

		configFileStrGet("/boa/my2.ini","sat",ini_sat,32);
		//printf("ini_sat = %s\r\n",ini_sat);

		configFileStrGet("/boa/my2.ini","sun",ini_sun,32);
		//printf("ini_sun = %s\r\n",ini_sun);

		configFileStrGet("/boa/my2.ini","rebootTimeSelect",ini_rebootTimeSelect,32);
		//printf("ini_rebootTimeSelect = %s\r\n",ini_rebootTimeSelect);

		if(strcmp(ini_maintenance,"open_auto")==0)
		{
			struct timeval tv;
			gettimeofday(&tv, NULL);
			time_t sec = tv.tv_sec;
			suseconds_t usec = tv.tv_usec;
			struct tm *lt = localtime(&sec);
			int sec_of_day = lt->tm_hour*60*60+lt->tm_min*60;
			//printf("%d %d\n", 
			//		lt->tm_wday,
			//		sec_of_day);
					
					

			const char s[2] = ":";
int count =0;
			char *token;
		   /* get the first token */
		   token = strtok(ini_rebootTimeSelect, s);
		   
		   /* walk through other tokens */
		   while( token != NULL ) {
			//  printf( " %s\n", token );
if(count == 0)
reboot_hour = atoi(token);
if(count == 1)
reboot_min = atoi(token);

count++;

			  token = strtok(NULL, s);
		   }
			
			int reboot_sec_of_day = reboot_hour*60*60+ reboot_min*60;

//printf("reboot_sec_of_day = %d\r\n",reboot_sec_of_day);


			if((strcmp(ini_mon,"open")==0)&&(lt->tm_wday == 1))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_mon reboot\r\n");
system("/sbin/reboot &");
				}

			}
			if((strcmp(ini_tues,"open")==0)&&(lt->tm_wday == 2))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_tues reboot\r\n");
system("/sbin/reboot &");
				}

			}
			if((strcmp(ini_wed,"open")==0)&&(lt->tm_wday == 3))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_wed reboot\r\n");
system("/sbin/reboot &");
				}

			}
			if((strcmp(ini_thurs,"open")==0)&&(lt->tm_wday == 4))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_thurs reboot\r\n");
system("/sbin/reboot &");
				}

			}
			
			if((strcmp(ini_fri,"open")==0)&&(lt->tm_wday == 5))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_fri reboot\r\n");
system("/sbin/reboot &");
				}

			}
			
			if((strcmp(ini_sat,"open")==0)&&(lt->tm_wday == 6))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_sat reboot\r\n");
system("/sbin/reboot &");
				}

			}
			
			if((strcmp(ini_sun,"open")==0)&&(lt->tm_wday == 0))
			{
				if(sec_of_day == reboot_sec_of_day)
				{
					
					printf("ini_sun reboot\r\n");
system("/sbin/reboot &");
				}

			}

		}

		else
		{
		;

		}
		usleep(500000);
		
	}



  

	return EXIT_SUCCESS;
}
